﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace NTime
{
    public class NetTime
    {
       private  string GetHttpWebRequest()
        {
            Uri uri = new Uri("http://www.hko.gov.hk/cgi-bin/gts/time5a.pr?a=1");
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(uri);
            myReq.UserAgent = "User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705";
            myReq.Accept = "*/*";
            myReq.KeepAlive = true;
            myReq.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5");
            HttpWebResponse result = (HttpWebResponse)myReq.GetResponse();
            Stream receviceStream = result.GetResponseStream();
            StreamReader readerOfStream = new StreamReader(receviceStream, System.Text.Encoding.GetEncoding("utf-8"));
            string strHTML = readerOfStream.ReadToEnd();
            readerOfStream.Close();
            receviceStream.Close();
            result.Close();
            return strHTML;
        }
        public DateTime getDT()
        {
            string TimeString = GetHttpWebRequest();
            long time = long.Parse(TimeString.Substring(2))/1000;//截取从第三个到最后一个，转换成秒          
            DateTime dt = new DateTime(1970, 1, 1).AddSeconds(time);
            DateTime dt1 = dt.AddHours(8);    //+8转为中国时区               
            return dt1;
            
        }
    }
}
